package vsvm.gui.menus;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;

import vsvm.gui.Environment;
import vsvm.gui.IUpdatable;
import vsvm.gui.objects.Workspace;

public class FileMenu extends JMenu implements IUpdatable {
	private final static String MF_NEW = "new";
	private final static String MF_OPEN = "open";
	private final static String MF_SAVE = "save";
	private final static String MF_SAVEAS = "saveas";
	private final static String MF_CLOSE = "close";
	private final static String MF_EXIT = "exit";
	
	private JMenuItem miNew = null;
	private JMenuItem miOpen = null;
	private JMenuItem miSave = null;
	private JMenuItem miSaveAs = null;
	private JMenuItem miClose = null;
	private JSeparator sep = null;
	private JMenuItem miExit = null;
	
	private class FileMenuActionListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals(MF_NEW)) {
				if (Environment.getEnvironment().getWorkspace() != null) {
					int opt = JOptionPane.showConfirmDialog(Environment.getEnvironment().getMainFrame(), "Save current workspace before creating new?");
					if (opt == JOptionPane.CANCEL_OPTION) return;
					else if (opt == JOptionPane.YES_OPTION)
						Environment.getEnvironment().getWorkspace().save();
				}
				Environment.getEnvironment().setWorkspace(new Workspace());
				return;
			}
			
			if (e.getActionCommand().equals(MF_OPEN)) {
				if (Environment.getEnvironment().getWorkspace() != null) {
					int opt = JOptionPane.showConfirmDialog(Environment.getEnvironment().getMainFrame(), "Save current workspace before creating new?");
					if (opt == JOptionPane.CANCEL_OPTION) return;
					else if (opt == JOptionPane.YES_OPTION)
						Environment.getEnvironment().getWorkspace().save();
				}
				JFileChooser fc = new JFileChooser(System.getProperty("."));
				if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(Environment.getEnvironment().getMainFrame())) {
					Workspace wsp = Workspace.loadFromFile(fc.getSelectedFile().getPath());
					Environment.getEnvironment().setWorkspace(wsp);
				}
				
				return;
			}
			
			if (e.getActionCommand().equals(MF_SAVE)) {
				Environment.getEnvironment().getWorkspace().save();
				return;
			}
			
			if (e.getActionCommand().equals(MF_SAVEAS)) {
				JFileChooser fc = new JFileChooser(System.getProperty("."));
				if (JFileChooser.APPROVE_OPTION == fc.showSaveDialog(Environment.getEnvironment().getMainFrame())) {
					Workspace wsp = Environment.getEnvironment().getWorkspace();
					wsp.setSaveFilePath(fc.getSelectedFile().getPath());
					wsp.save();
				}
				return;
			}
			
			if (e.getActionCommand().equals(MF_CLOSE)) {
				if (Environment.getEnvironment().getWorkspace() != null) {
					int opt = JOptionPane.showConfirmDialog(Environment.getEnvironment().getMainFrame(), "Save current workspace before creating new?");
					if (opt == JOptionPane.CANCEL_OPTION) return;
					else if (opt == JOptionPane.YES_OPTION)
						Environment.getEnvironment().getWorkspace().save();
				}
				Environment.getEnvironment().setWorkspace(null);
				return;
			}
			
			if (e.getActionCommand().equals(MF_EXIT)) {
				Environment.getEnvironment().getMainFrame().dispose();
				return;
			}			
		}		
	}
	
	public FileMenu() {
		super();
		this.setText(Environment.getEnvironment().getResource().getString("File"));
		createItems();		
	}
	
	private void createItems() {
		ActionListener ml = new FileMenuActionListener();
		
		miNew = new JMenuItem("New");
		miNew.setActionCommand(MF_NEW);
		miNew.addActionListener(ml);
		
		miOpen = new JMenuItem("Open...");
		miOpen.setActionCommand(MF_OPEN);
		miOpen.addActionListener(ml);
		
		miSave = new JMenuItem("Save");
		miSave.setActionCommand(MF_SAVE);
		miSave.addActionListener(ml);
		
		miSaveAs = new JMenuItem("Save As...");
		miSaveAs.setActionCommand(MF_SAVEAS);
		miSaveAs.addActionListener(ml);
		
		miClose = new JMenuItem("Close");
		miClose.setActionCommand(MF_CLOSE);
		miClose.addActionListener(ml);
		
		sep = new JSeparator();
		
		miExit = new JMenuItem("Exit");
		miExit.setActionCommand(MF_EXIT);
		miExit.addActionListener(ml);
		
		this.add(miNew);
		this.add(miOpen);
		this.add(miSave);
		this.add(miSaveAs);
		this.add(miClose);
		this.add(sep);
		this.add(miExit);
	}
	
	public void update() {
		if (Environment.getEnvironment().getWorkspace() == null) {
			miSave.setEnabled(false);
			miSaveAs.setEnabled(false);
			miClose.setEnabled(false);
		} else {
			miSave.setEnabled(true);
			miSaveAs.setEnabled(true);
			miClose.setEnabled(true);			
		}
	}

}
